/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.ipfw.cs590.cheat.struts.actionforms;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.util.LabelValueBean;
import org.web_cat._1_0.assignment_sharing.Assignment;

/**
 *
 * @author SafarTimuro
 */
public class AssignmentSearchForm extends ActionForm {

    /**
     * Enumeration of suppported search types
     */
    public enum SearchType {

        /**
         * Search by course.
         */
        Course,
        /**
         * Search by keyword
         */
        Keyword,
        /**
         * Search by language
         */
        Language,
        /**
         * Search by UUID
         */
        UUID;
    }
    /**
     * Search type
     */
    private SearchType searchType;
    /**
     * Search query
     */
    private String query;
    /**
     * Search results
     */
    private List<Assignment> searchResults;

    /**
     * @return the searchType
     */
    public String getSearchType() {
        return searchType.toString();
    }

    /**
     * @param searchType the searchType to set
     */
    public void setSearchType(String searchType) {
        this.searchType = SearchType.valueOf(searchType);
    }

    /**
     * @return the searchResults
     */
    public List<Assignment> getSearchResults() {
        return searchResults;
    }

    /**
     * @param searchResults the searchResults to set
     */
    public void setSearchResults(List<Assignment> searchResults) {
        this.searchResults = searchResults;
    }

    /**
     * @return the query
     */
    public String getQuery() {
        return query;
    }

    /**
     * @param query the query to set
     */
    public void setQuery(String query) {
        this.query = query;
    }

    /**
     * Return the list of query types.
     * @return List query results.
     */
    public List getQueryList() {

        // build the duration list based on the duration value
        List durationList = new ArrayList();

        //Assemble list of query tpyes.
        for (SearchType v : SearchType.values()) {
            LabelValueBean lvb = new LabelValueBean(v.name(), v.name());
            durationList.add(lvb);
        }
        return durationList;
    }

    /**
     * Constructor
     */
    public AssignmentSearchForm() {
        super();
        this.query = "";
        this.searchType = SearchType.Course;
        this.searchResults = null;
    }

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param request The HTTP Request we are processing.
     * @return
     */
    @Override
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
        ActionErrors errors = new ActionErrors();
        //If the search type is invalid return null
        if (searchType == null) {
            errors.add("searchTypeError", new ActionMessage("errors.search.option.invalid"));
        }
        return errors;
    }
}
